/*
 * Copyright (C), 2002-2017, 南京苏宁软件技术有限公司
 * FileName: ExchangeController.java
 * Author:   10070706
 * Date:     2017年5月3日 上午9:21:28
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller;

import java.security.Principal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.exchange.CouponDetailInfoDto;
import com.suning.sawp.dto.exchange.ExchangeCouponResponseDto;
import com.suning.sawp.dto.exchange.ExchangeOrderResponseDto;
import com.suning.sawp.dto.exchange.SendCouponRecordDto;
import com.suning.sawp.intf.exchange.ExchangeOrderCouponListQueryService;
import com.suning.sawp.intf.exchange.ExchangeOrderSendCouponService;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 〈以旧换新发券入口controller〉<br>
 *
 * @author 10070706
 */
@Controller
@RequestMapping("/exchange")
public class ExchangeController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExchangeController.class);

    @Resource(name = "exchangeOrderSendCouponService")
    private ExchangeOrderSendCouponService exchangeOrderSendCouponService;

    @Autowired
    private ExchangeOrderCouponListQueryService exchangeOrderCouponListQueryService;

    /**
     * 功能描述: 获取订单列表<br>
     *
     * @param mobileNo
     * @param pageNo
     * @return
     */
    @RequestMapping("/queryOrderList.do")
    public MyGsonView queryOrderList(String mobileNo, Integer pageNo) {
        String decodeMobile = AESUtils.decodeAndDecrypt(mobileNo, ConfigConstants.AES_KEY);
        LOGGER.info("查询以旧换新订单列表,手机号:{},页码:{}", decodeMobile, pageNo);
        MyGsonView mgv = createGsonView();
        if (StringUtils.isEmpty(decodeMobile) || null == pageNo || pageNo <= 0) {
            setErrorMsg(mgv, ErrorCodeConstants.EXCHANGE_REQ_ILLEGAL);
            return mgv;
        }

        try {
            ReturnMsg<ExchangeOrderResponseDto> ret = exchangeOrderCouponListQueryService.queryOrderList(decodeMobile,
                    pageNo);
            mgv = setGsonView(ret);
        } catch (Exception e) {
            LOGGER.error("查询以旧换新订单列表异常:", e);
            setErrorMsg(mgv, ErrorCodeConstants.ERR_0002);
        }
        return mgv;
    }

    /**
     * 功能描述: 查询可发券列表<br>
     *
     * @param mobileNo
     * @param pageNo
     * @return
     */
    @RequestMapping("/queryCouponList.do")
    public MyGsonView queryCouponList(String orderNo, Principal principal) {
        String employeeId = principal.getName();
        LOGGER.info("查询可发券列表,订单号:{},员工工号:{}", orderNo, employeeId);
        MyGsonView mgv = createGsonView();
        if (StringUtils.isEmpty(orderNo)) {
            setErrorMsg(mgv, ErrorCodeConstants.EXCHANGE_REQ_ILLEGAL);
            return mgv;
        }

        try {
            ReturnMsg<ExchangeCouponResponseDto> ret = exchangeOrderCouponListQueryService.queryCouponList(orderNo,
                    employeeId);
            mgv = setGsonView(ret);
        } catch (Exception e) {
            LOGGER.error("查询以旧换新发券列表异常:", e);
            setErrorMsg(mgv, ErrorCodeConstants.ERR_0002);
        }
        return mgv;
    }

    /**
     * 功能描述: 查询券规则<br>
     *
     * @param activityId 活动编号
     * @param activitySecretKey 活动秘钥
     * @return
     */
    @RequestMapping("/queryCouponRules.do")
    public MyGsonView queryCouponRules(HttpServletRequest httpServletRequest, String activityId,
            String activitySecretKey) {
        ReturnMsg<CouponDetailInfoDto> result = exchangeOrderSendCouponService
                .queryCouponDetailInfo(httpServletRequest.getRemoteUser(), activityId, activitySecretKey);

        return setGsonView(result);
    }

    /**
     * 功能描述: 发券执行<br>
     *
     * @param mobileNo 手机号码
     * @param orderNo 订单号
     * @param activityId 活动编号
     * @param activitySecretKey 活动秘钥
     * @return
     */
    @RequestMapping("/sendCoupon.do")
    public MyGsonView sendCoupon(HttpServletRequest request, String mobileNo, String orderNo, String activityId,
            String activitySecretKey) {
        ReturnMsg<String> result = exchangeOrderSendCouponService.sendCoupon(request.getRemoteUser(), mobileNo, orderNo,
                activityId, activitySecretKey);
        return setGsonView(result);
    }

    /**
     * 功能描述: 根据员工工号，查询发券记录<br>
     *
     * @param pageNo 页码
     * @return
     */
    @RequestMapping("/queryCouponRecords.do")
    public MyGsonView queryCouponRecords(HttpServletRequest request, Integer pageNo) {
        ReturnMsg<SendCouponRecordDto> result = exchangeOrderSendCouponService
                .querySendCouponRecord(request.getRemoteUser(), pageNo);
        return setGsonView(result);
    }
}
